package com.huahua.graduateschoolspringboot.server;


import cn.hutool.core.util.HexUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        log.info("初始化 SocketChannel");

        ChannelPipeline pipeline = ch.pipeline();
        //解决拆包问题7 = -17
        pipeline.addLast(new DelimiterBasedFrameDecoder(1024, Unpooled.wrappedBuffer("\r\n".getBytes())));
        // 自定义解码器
        pipeline.addLast(new MyDecoder());
        // 自定义编码器
        pipeline.addLast(new MyEncoder());
        // 自定义hanler 处理解码消息并回复信息
        pipeline.addLast(new SocketHandler());
    }

}
